package o2o.web.actions.tip;

import o2o.config.dao.UserDaoSql;
import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.tip.TipImageConfigMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.tip.TipImageConfig;
import o2o.dao.PO.user.UserInfo;
import o2o.web.actions.user.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.List;

//工单标签管理
@Controller
public class TipImageAction {


    //获取图片配置
    @RequestMapping(method = RequestMethod.POST, value = "/tip/image/list")
    @ResponseBody
    public String goods_label_list() {

        List<TipImageConfig> list = TipImageConfigMemory.getInstance().listOfAll();
        if (ValidateUtil.isAbsList(list)) {
            return AjaxResult.successResult(list.size(), list);
        }

        return AjaxResult.successResult(0, null);
    }


    //----------------------------------------管理
    //新增
    @RequestMapping(method = RequestMethod.POST, value = "/admin/tip/image/add")
    @ResponseBody
    public String shop_label_add(String token, TipImageConfig info) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        if (null == info) {
            return AjaxResult.errorResult("信息不完整，请填写完整");
        }

        if (ValidateUtil.isNull(info.getName())) {
            return AjaxResult.errorResult("请输入名称");
        }

        if (null == info.getMinCount()) {
            info.setMinCount(0);
        }
        if (null == info.getMaxCount()) {
            info.setMaxCount(-1);
        }
        if (null == info.getIsMust()) {
            info.setIsMust(0);
        }

        long time = DateUtils.getCurrTime();
        info.setCreateTime(time);
        info.setDelFlag(0);
        info.setCreateUser(user.getUsername());

        //存入数据库
        try {
            UserDaoSql.getInstance().insertObj(PojoTables.TIP_IMAGE_CONFIG, info);
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("新增失败，请稍后再试");
        }

        //存入缓存
        TipImageConfigMemory.getInstance().add(time);

        return AjaxResult.successResult();
    }

    //修改
    @RequestMapping(method = RequestMethod.POST, value = "/admin/tip/image/modify")
    @ResponseBody
    public String shop_label_modify(String token, TipImageConfig info) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        if (null == info) {
            return AjaxResult.errorResult("信息不完整，请填写完整");
        }

        if (ValidateUtil.isNotID(info.getId())) {
            return AjaxResult.errorResult("请选择要修改的");
        }

        TipImageConfig tipImageConfig = TipImageConfigMemory.getInstance().getById(info.getId());
        if (null == tipImageConfig) {
            return AjaxResult.errorResult("您修改的已不存在，请新增");
        }

        if (null == info.getName()) {
            info.setName(tipImageConfig.getName());
        }
        if (null == info.getMinCount()) {
            info.setMinCount(tipImageConfig.getMinCount());
        }
        if (null == info.getMaxCount()) {
            info.setMaxCount(tipImageConfig.getMaxCount());
        }
        if (null == info.getIsMust()) {
            info.setIsMust(tipImageConfig.getIsMust());
        }


        if (ValidateUtil.isNull(info.getName())) {
            return AjaxResult.errorResult("请输入类别名称");
        }


        //修改商品类别
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.TIP_IMAGE_CONFIG).append(" set ")
                .append(" name='").append(info.getName()).append("',note='").append(info.getNote())
                .append("',updateUser='").append(user.getUsername()).append("' ")
                .append(",descs='").append(info.getDescs()).append("',placeholder='").append(info.getPlaceholder()).append("'")
                .append(",isMust=").append(info.getIsMust()).append(",minCount=").append(info.getMinCount()).append(",maxCount=").append(info.getMaxCount())
                .append(",preName='").append(info.getPreName()).append("'")
                .append(",nameFontColor='").append(info.getNameFontColor()).append("',nameFontSize=").append(info.getNameFontSize()).append(",nameBackgroundColor='").append(info.getNameBackgroundColor()).append("'")
                .append(",placeholderFontColor='").append(info.getPlaceholderFontColor()).append("',placeholderFontSize=").append(info.getPlaceholderFontSize()).append(",placeholderBackgroundColor='").append(info.getPlaceholderBackgroundColor()).append("'")
                .append(",note='").append(info.getNote()).append("',extra1='").append(info.getExtra1()).append("',extra2='").append(info.getExtra2()).append("',extra3='").append(info.getExtra3()).append("'")
                .append(" where id = ").append(info.getId()).append(" and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("修改失败，请稍后再试");
        }

        //修改缓存
        TipImageConfigMemory.getInstance().modify(info.getId());

        return AjaxResult.successResult();
    }


    //删除
    @RequestMapping(method = RequestMethod.POST, value = "/admin/tip/image/delete")
    @ResponseBody
    public String shop_label_delete(String token, Long id) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

        //验证是否有登录权限
        if (!User.hasAdminAuth(user.getUserType())) {
            return AjaxResult.errorResult("您没有操作权限");
        }

        if (ValidateUtil.isNotID(id)) {
            return AjaxResult.errorResult("请选择要删除的");
        }

        TipImageConfig tipImageConfig = TipImageConfigMemory.getInstance().getById(id);
        if (null == tipImageConfig) {
            return AjaxResult.errorResult("您要删除的已不存在");
        }

        //删除商品类别
        StringBuilder sb = new StringBuilder();
        sb.append("update ").append(PojoTables.TIP_IMAGE_CONFIG).append(" set delFlag = 1,updateUser='").append(user.getUsername())
                .append("' where id = ").append(id).append(" and delFlag = 0");
        try {
            UserDaoSql.getInstance().executeBySql(sb.toString());
        } catch (SQLException e) {
            e.printStackTrace();
            return AjaxResult.errorResult("删除失败，请稍后再试");
        }

        //在缓存中删除
        TipImageConfigMemory.getInstance().delete(id);

        return AjaxResult.successResult();
    }

}
